package lp.xian.materials.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lp.xian.materials.domain.Stock;
import lp.xian.materials.service.IStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存商品Controller
 *
 * @author ruoyi
 * @date 2023-08-18
 */
@RestController
@RequestMapping("/stocktb/stocktb")
public class StockController extends BaseController
{
    @Autowired
    private IStockService stockService;

    /**
     * 查询库存商品列表
     */
    @PreAuthorize("@ss.hasPermi('stocktb:stocktb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Stock stock)
    {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 导出库存商品列表
     */
    @PreAuthorize("@ss.hasPermi('stocktb:stocktb:export')")
    @Log(title = "库存商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stock stock)
    {
        List<Stock> list = stockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        util.exportExcel(response, list, "库存商品数据");
    }

    /**
     * 获取库存商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('stocktb:stocktb:query')")
    @GetMapping(value = "/{warehouseStockTbId}")
    public AjaxResult getInfo(@PathVariable("warehouseStockTbId") Integer warehouseStockTbId)
    {
        return success(stockService.selectStockByWarehouseStockTbId(warehouseStockTbId));
    }

    /**
     * 新增库存商品
     */
    @PreAuthorize("@ss.hasPermi('stocktb:stocktb:add')")
    @Log(title = "库存商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stock stock)
    {
        return toAjax(stockService.insertStock(stock));
    }

    /**
     * 修改库存商品
     */
    @PreAuthorize("@ss.hasPermi('stocktb:stocktb:edit')")
    @Log(title = "库存商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stock stock)
    {
        return toAjax(stockService.updateStock(stock));
    }

    /**
     * 删除库存商品
     */
    @PreAuthorize("@ss.hasPermi('stocktb:stocktb:remove')")
    @Log(title = "库存商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehouseStockTbIds}")
    public AjaxResult remove(@PathVariable Integer[] warehouseStockTbIds)
    {
        return toAjax(stockService.deleteStockByWarehouseStockTbIds(warehouseStockTbIds));
    }
}
